$(function () {
    container.off("click");
    function getDepartmentsByName(page) {
        axios({
            url:url,
            params:{
                "async":true,
                "page":page,
                "departmentName":$("#searchName").val()
            }
        }).then(function (response) {
            $("#mainContainer").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    }
    //分页
    $.tbpage("#mainContainer",function (page) {
        getDepartmentsByName(page)
    });

    //搜索
    $("#searchNameBtn").click(function () {
        getDepartmentsByName(0)
    });

    // 获取添加院部的界面
    $("#addDepartment").click(function() {
        $("#departmentDialogTitle").html("添加院部");
        axios({
            method:'get',
            url: "/department/add"
        }).then(function (response) {
            $("#departmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //获取编辑院部界面
    container.on("click","#edit-department",function () {
        $("#departmentDialogTitle").html("编辑院部");
        axios({
            method:'get',
            url:"/department/edit/"+$(this).attr('departmentId')
        }).then(function (response) {
            $("#departmentForm").html(response.data);
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //提交器材操作，清空表单
    $("#departmentForm").submit(function() {
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:"POST",
            url:"/department",
            data:$('#departmentForm').serialize(),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            $('#departmentForm')[0].reset();
            if (response.data.success) {
                // 从新刷新主界面
                getDepartmentsByName(0);
                toastr.success(response.data.message);
            }else { toastr.error(response.data.message);}
        }).catch(function (error) {
            toastr.error("error!");
        });

        $("#departmentDialogClose").click();
        return false;
    });


    //删除院部
    container.on("click","#delete-department",function () {
        // 获取 CSRF Token
        var csrfToken = $("meta[name='_csrf']").attr("content");
        axios({
            method:'DELETE',
            url:"/department/"+$(this).attr('departmentId'),
            headers:{'X-CSRF-TOKEN':csrfToken}
        }).then(function (response) {
            if (response.data.success) {
                // 从新刷新主界面
                getDepartmentsByName(0);
                toastr.success(response.data.message);
            } else {
                toastr.error(response.data.message);
            }
        }).catch(function (error) {
            toastr.error("error!");
        });
    });

    //关闭Dialog界面
    $("#departmentDialogClose").click(function () {
        $("#departmentForm").html("");
    });

});